package com.iotechn.unimall.admin.api.integral;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.iotechn.unimall.biz.service.integral.IntegralBizService;
import com.iotechn.unimall.core.Const;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.component.CacheComponent;
import com.iotechn.unimall.data.domain.IntegralTaskDO;
import com.iotechn.unimall.data.enums.IntegralTaskOriType;
import com.iotechn.unimall.data.mapper.IntegralTaskMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author kyms
 * @date 2020-08-04 17:19
 */
@Service
public class AdminIntegralTaskServiceImpl implements AdminIntegralTaskService {

    private static final String CA_INTEGRALTASK_DTO_KEY = "CA_INTEGRALTASK_DTO_KEY";

    private static final Logger logger = LoggerFactory.getLogger(AdminIntegralTaskServiceImpl.class);

    @Autowired
    private IntegralTaskMapper integralTaskMapper;

    @Autowired
    private CacheComponent cacheComponent;

    @Autowired
    private IntegralBizService integralBizService;

    @Override
    public List<IntegralTaskDO> listTask(Long adminId) {
        int n=0;
        for (IntegralTaskOriType type : IntegralTaskOriType.class.getEnumConstants()){
            n++;
        }
        List<IntegralTaskDO> taskDOS = new LinkedList<>();
        List<IntegralTaskDO> taskList = cacheComponent.getObjList(CA_INTEGRALTASK_DTO_KEY, IntegralTaskDO.class);
        if (taskList == null || taskList.size() == 0) {
            List<IntegralTaskDO> integralTaskDOS = integralTaskMapper.selectList(new EntityWrapper<IntegralTaskDO>()
                    .setSqlSelect("ifnull(integral,0) as integral", "type", "ifnull(integral_title,'') as integralTitle",
                            "ifnull(integral_desc,'') as integralDesc",
                            "ifnull(icon,'') as icon", "status","id"));
            List<Long> taskids = integralTaskDOS.stream().map(e -> e.getId()).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(integralTaskDOS) || integralTaskDOS.size() < n) {
                Date now = new Date();
                integralTaskDOS = new LinkedList<>();
                for (IntegralTaskOriType type : IntegralTaskOriType.class.getEnumConstants()) {
                    IntegralTaskDO task = new IntegralTaskDO();
                    task.setId(type.getId());
                    task.setIntegral(type.getIntegral());
                    task.setIcon(type.getIcon());
                    task.setType(type.getType());
                    task.setIntegralTitle(type.getIntegralTitle());
                    task.setIntegralDesc(type.getIntegralDesc());
                    task.setGmtCreate(now);
                    task.setGmtUpdate(now);
                    task.setAdminId(1L);
                    task.setStatus(1);
                    integralTaskDOS.add(task);
                    taskDOS = integralTaskDOS;
                    if(taskids.contains(type.getId())){
                        continue;
                    }
                    integralTaskMapper.insertHaveId(task);
                }
            }
            cacheComponent.putObj(CA_INTEGRALTASK_DTO_KEY, integralTaskDOS, Const.CACHE_ONE_DAY);
        } else {
            return taskList;
        }
        return taskDOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public IntegralTaskDO updateTask(IntegralTaskDO task, Long adminId) throws ServiceException {
        if (task == null || task.getId() == null) {
            throw new AdminServiceException(ExceptionDefinition.PARAM_CHECK_FAILED);
        }
        Date now = new Date();
        task.setGmtUpdate(now);
        task.setGmtCreate(now);
        integralTaskMapper.updateById(task);
        //更新
        integralBizService.initIntegralTask();
        return task;
    }
}
